﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace SpaceArcade
{
    class Player : AnimatedSprite
    {        
        private bool[] keysDown;       
        public bool isDead = false;
        public bool stopGame = false;
        private Bomb DataBomb;

        public bool PowerUpedBlast = false;
        public int ShieldDurbility;
        public Shield spaceShield;
      
        public int HitPoints
        {
            get;
            set;
        }

        public List<Bomb> Bombs
        {
            get;
            set;
        }

        public Player(int x, int y, string path = "strip_player.png", int framesCount = 64)
            :base (path, x, y, framesCount)
        {
            HitPoints = 3;
            keysDown = new bool[256];
            Bombs = new List<Bomb>();
            DataBomb = new Bomb(-50, 50);            
            snd = new GameSound();
            
        }

        public override bool Intersects(AnimatedSprite object2)
        {
            if (!isDead)
            {
                if (object2 != null)
                {
                    if (spaceShield == null)
                    {
                        if ((this.X + this.FrameWidth - 60 < object2.X || object2.X + object2.FrameWidth - 60 < this.X || this.Y + this.FrameHeight / 2 - 20 < object2.Y || object2.Y + object2.FrameHeight / 2 < this.Y) &&
                            (this.X + this.FrameWidth - 45 < object2.X || object2.X + object2.FrameWidth - 45 < this.X || this.Y + this.FrameHeight < object2.Y + 35 || object2.Y + object2.FrameHeight - 27 < this.Y + 52))
                            return false;
                        else
                        {
                            
                            snd.PlayHurtSoundWithShield();
                            if (HitPoints <= 1)
                                snd.PlayExplisionSound();
                            if (ShieldDurbility <= 0)
                            {
                                this.HitPoints--;
                                PowerUpedBlast = false;
                                DataBomb.ChangeTexture("thrust_blue.png", 24);
                            }
                            else
                            {
                                ShieldDurbility--;
                                if (ShieldDurbility < 1)
                                    spaceShield = null;
                            }
                            return true;
                        }
                    } else
                    {
                        if ((this.X + this.FrameWidth - 20 < object2.X || object2.X + object2.FrameWidth - 20 < this.X || this.Y + this.FrameHeight / 2 - 10 < object2.Y || object2.Y + object2.FrameHeight / 2 < this.Y) &&
                            (this.X + this.FrameWidth - 15 < object2.X || object2.X + object2.FrameWidth - 15 < this.X || this.Y + this.FrameHeight < object2.Y + 15 || object2.Y + object2.FrameHeight - 17 < this.Y + 22))
                            return false;
                        else
                        {
                            
                            
                            
                                snd.PlayHurtSound();
                            if (ShieldDurbility <= 0)
                            {
                                this.HitPoints--;
                                PowerUpedBlast = false;
                                DataBomb.ChangeTexture("thrust_blue.png", 24);
                            }
                            else
                            {
                                ShieldDurbility--;
                                if (ShieldDurbility < 1)
                                    spaceShield = null;
                            }
                           
                            return true;
                        }

                    }
                }
                else
                    return false;
            }
            else
                return false;
        }


       

        public bool Intersects(PowerUP object2)
        {            
            if (!isDead)
            {
                if (object2 != null)
                {
                    if ((this.X + this.FrameWidth - 60 < object2.X || object2.X + object2.bm.Width - 60 < this.X || this.Y + this.FrameHeight / 2 - 20 < object2.Y || object2.Y + object2.bm.Height / 2 < this.Y) &&
                        (this.X + this.FrameWidth - 45 < object2.X || object2.X + object2.bm.Width - 45 < this.X || this.Y + this.FrameHeight < object2.Y + 35 || object2.Y + object2.bm.Height - 27 < this.Y + 52))
                        return false;
                    else
                    {
                        if (object2 is Shield)
                        {                            
                            ShieldDurbility = 3;
                            spaceShield = new Shield("Shield.png", this.X - 10, this.Y - 10);
                        }else if(object2 is BonusLife)
                        {
                            this.HitPoints++;
                        }
                        else
                        {
                            PowerUpedBlast = true;
                            DataBomb.ChangeTexture("thrust_green&purple.png", 24);
                        }
                        snd.PowerUpSound();
                        
                        return true;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        } 
      
        public void Explode()
        {
            if (!isDead)
            {
                ChangeTexture("exp_type_A.png", 40);
                isDead = true;                
            }
            if (CurrFrame == lastFrame - 1)
                stopGame = true;      

        }

        public override void Draw(System.Drawing.Bitmap gameBM)
        {
            base.Draw(gameBM);
            foreach (Bomb bombs in Bombs)
                bombs.Draw(gameBM);

            if (spaceShield != null)
                spaceShield.Draw(gameBM);
        }

        public virtual void OnKeyDown(KeyEventArgs e,int state = 0)
        {
            keysDown[(int)e.KeyCode] = true;
        }
        GameSound snd;
        public virtual void OnKeyUp(KeyEventArgs e, int state = 0)
        {
            
            if (!isDead)
            {
                if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
                {
                    this.SetFramesRange(33, 48);
                }
                keysDown[(int)e.KeyCode] = false;
                if (e.KeyCode == Keys.Space)
                {
                    Bombs.Add(new Bomb(this.X, this.Y, DataBomb.bm));
                    if (!PowerUpedBlast)
                    {
                        snd.BlastSound();
                    }else
                    {
                        snd.PowerBlastSound();
                    }
                }
            }
        }

        public void Move(Form f)
        {
            if (!isDead)
            {
                if (keysDown[(int)Keys.Left])
                {
                    this.x -= 10;
                    this.SetFramesRange(1, 16);
                }
                if (keysDown[(int)Keys.Right])
                {
                    this.x += 10;
                    this.SetFramesRange(17, 32);
                }
                if (keysDown[(int)Keys.Up])
                {
                    this.y -= 7;
                }
                if (keysDown[(int)Keys.Down])
                {
                    this.y += 7;
                }
                if (this.x + 100 >= f.Width)
                {
                    this.x -= 10;
                }
                if (this.x + 35 <= 0)
                {
                    this.x += 10;
                }
                if (this.y + 145 >= f.Height)
                {
                    this.y -= 7;
                }
            }
        }      
    }

         class Bomb : AnimatedSprite
        {            
           
            bool destroyed = false;
            static int _FWidth, _FHeight;
            public Bomb(int x, int y, string path = "thrust_blue.png", int framesCount = 24)                
            {
                this.x = x;
                this.y = y;

               

                _bm = new Bitmap(Image.FromFile(GamePath.TexturesPath + path));
                bm = new List<Bitmap>();

                int currX = 0;
                _frameHeight = _bm.Height;
                _frameWidth = _bm.Width / framesCount;
                while (currX < _bm.Width - _frameWidth)
                {
                    Rectangle rect = new Rectangle(currX, 0, _frameWidth, _frameHeight);
                    bm.Add(_bm.Clone(rect, _bm.PixelFormat));
                    currX += _frameWidth;
                }
                _FWidth = _frameWidth;
                _FHeight = _frameHeight;
            }

            public Bomb(int x, int y, List<Bitmap> bm)
            {
                this.X = x;
                this.Y = y;
                this.bm = bm;
                st = new Stopwatch();
            }
         
            public void Move()
            {
                y -= 25;               
            }

            
            public override bool Intersects(AnimatedSprite object2)
            {
                if (!destroyed)
                {
                    if (this.X + _FWidth - 45 < object2.X || object2.X + object2._frameWidth - 45 < this.X || this.Y + _FHeight < object2.Y || object2.Y + object2._frameHeight - 27 < this.Y)
                        return false;
                    else
                        return true;
                }
                else
                    return false;
            }
        }
    }

